package com.umpay.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLDecoder;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EhcacheUtil {
//	private static Logger logger = LoggerFactory.getLogger(EhcacheUtil.class);
//	private static CacheManager cacheManager;
//	private static String fileName = "config/ehcache.xml";
//	
//	static{
//		load();
//	}
//	
//	public static Cache getCache(String cacheName,Class<?> clazz) throws CacheException{
//		if(cacheManager==null){
//			load();
//		}
//		return cacheManager.getCache(cacheName);
//	}
//	
//	public static Cache getCache(String cacheName) throws CacheException{
//		if(cacheManager==null){
//			InputStream in = Resource.loadResource(fileName);
//			cacheManager = CacheManager.create(in);
//			
//		}
//		return cacheManager.getCache(cacheName);
//	}
//	
//	@SuppressWarnings("deprecation")
//	private static void load(){
//		logger.info("load Ehcache");
//		InputStream in = null;
//		try{
//			logger.debug("fileName==" + fileName);
//			URL url = EhcacheUtil.class.getClassLoader().getResource(fileName);
//			logger.debug("url=========" + url);
//			String path = URLDecoder.decode(url.getPath());
//			logger.info("配置文件的绝对路径：" + path);
//			File file = new File(path);
//			if (!file.exists()) {
//				throw new Exception("ehcache配置文件不存在：" + path.substring(path.indexOf("/") + 1));
//			}
//			in = new FileInputStream(file);
//			cacheManager = CacheManager.create(in);
//		}catch(Exception e){
//			logger.error("获取ehcache配置信息失败",e);
//		}finally{
//			if(in != null){
//				try {
//					in.close();
//				} catch (IOException e) {
//					logger.error("释放ehcache配置文件资源失败!", e);
//				}
//				in = null;
//			}
//		}
//	}
//	
//	public static void main(String[] args) {
////		Cache cache = EhcacheUtil.getCache("SenRetCode");
////		cache.put(new Element("123","123"));
////		System.out.println(cache.get("123").getValue());
////		System.out.println(PropertiesHelper.getFailRate("funcode.default"));
//	}
}
